package com.lagou.analysis.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;


@ControllerAdvice
public class GlobalResponseBodyAdvice implements ResponseBodyAdvice<Object> {

	private final Logger logger = LoggerFactory.getLogger(GlobalResponseBodyAdvice.class);

	@Override
	public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
		return true;
	}

	@Override
	public Object beforeBodyWrite(Object obj, MethodParameter methodParameter, MediaType mediaType,
	                              Class<? extends HttpMessageConverter<?>> aClass,
	                              ServerHttpRequest serverHttpRequest,
	                              ServerHttpResponse serverHttpResponse) {
		if (obj == null || !(obj instanceof Msg)) {
			obj = new Msg<Object>(obj);
		}
		return obj;
	}

}